package com.sevlet;

import com.Bean.*;
import com.dao.CheckInDao;
import com.dao.impl.CheckInDaoImpl;
import com.dao.impl.GoodsDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.util.JsonUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/RentSevlet")
public class RentSevlet extends BaseSevlet{
    //返回消费信息({消费类型名,消费商品[id,name,...]})
    public void getxiaofei(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsDaoImpl goodsDao = new GoodsDaoImpl();
        JsonUtil jsonUtil =new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        PrintWriter writer = resp.getWriter();
        try {
            List<GoodsAndType> goodsAndTypes = goodsDao.queryGoodsandType();

            jsonUtil.setCode(0);
            jsonUtil.setMsg("ok");
            ArrayList<GoodsAndType1> sort = Sort(goodsAndTypes);
            jsonUtil.setData(sort);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }finally {
            writer.print(gson.toJson(jsonUtil));
        }

    }
    private ArrayList<GoodsAndType1> Sort(List<GoodsAndType> goodsAndTypes){
        String Gtype="";
        ArrayList<Goods> goods1 = new ArrayList<>();//商品列表
        GoodsAndType1 goodsAndType1 = new GoodsAndType1();
        ArrayList<GoodsAndType1> goodsAndType1s = new ArrayList<>();
        Goods goods = null;
        for (GoodsAndType goodsAndType : goodsAndTypes) {
            if (Gtype==""||goodsAndType.getGtype().equals(Gtype)){
                goods = new Goods();
                goods=goodsAndType.getGoods();
                goods1.add(goods);
                Gtype=goodsAndType.getGtype();
            }else if (!goodsAndType.getGtype().equals(Gtype)){
                goodsAndType1.setGtype(Gtype);
                goodsAndType1.setGoodss(goods1);
                goodsAndType1s.add(goodsAndType1);

                Gtype=goodsAndType.getGtype();
                goodsAndType1 = new GoodsAndType1();
                goods1 = new ArrayList<>();
                goods = new Goods();
                goods=goodsAndType.getGoods();
                goods1.add(goods);
                Gtype=goodsAndType.getGtype();
            }

        }
        System.out.println("goodsAndType:"+goodsAndType1s);
        return goodsAndType1s;
    }
    public void renRoom(HttpServletRequest req, HttpServletResponse resp)  {
        //房间
        String zyfj = req.getParameter("zyfj");

        //房间状态
        String rstatus = req.getParameter("rstatus");
        System.out.println("rstatus:"+rstatus);
        //个人信息
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String idcard = req.getParameter("idcard");
        String timetype = req.getParameter("timetype");
        String username = req.getParameter("username");
        System.out.println("user:"+username);


        //其他消费
        String goods = req.getParameter("goods");//其他消费项
        String sunmoney = req.getParameter("sunmoney");//订单总金额
        String out_status = req.getParameter("out_status");//账单状态
        String othermoney = req.getParameter("othermoney");//其他消费总金额
        String yajin = req.getParameter("yajin");//押金
        String ltype = req.getParameter("ltype");//入住类型
        //支付方式
        String payitem = req.getParameter("payitem");

        System.out.println("zyfj:"+zyfj);

        System.out.println("name:"+name);
        System.out.println("phone:"+phone);
        System.out.println("idcard:"+idcard);
        System.out.println("goods:"+goods);
        System.out.println("othermoney:"+othermoney);
        System.out.println("out_status:"+out_status);
        System.out.println("sunmoney:"+sunmoney);
        System.out.println("yajin:"+yajin);

        ren(req,resp,zyfj,rstatus,name,phone,idcard,username,goods,sunmoney,out_status,othermoney,yajin,ltype,payitem,timetype);

    }
    public void ren(HttpServletRequest req, HttpServletResponse resp,String zyfj,String rstatus,String name,String phone,String idcard,String username,String goods,String sunmoney,String out_status,String othermoney,String yajin,String ltype,String payitem,String timetype){

        Boolean flag=true;//真:存在客户
        CheckInDaoImpl checkInDao1 = new CheckInDaoImpl();
        Client client1 =null;
        try {
            client1 = checkInDao1.SelectIdCard(idcard);
            if (client1==null){
                flag=false;
                //不存在则新创建顾客
                client1 = new Client();
                client1.setPno((System.currentTimeMillis()+"").substring(5));
                client1.setCliPid("1");
                client1.setPname(name);
                client1.setPphone(phone);
                client1.setPid(idcard);
                client1.setPtotal(Double.parseDouble(sunmoney));
                client1.setPcount(1);
                client1.setLasttime(new Date());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String format = simpleDateFormat.format(new Date());
                client1.setPcretime(format);

            }else {
                client1.setPtotal(client1.getPtotal()+Double.parseDouble(sunmoney));
                client1.setLasttime(new Date());
                client1.setPcount(client1.getPcount()+1);
                client1.setPid(idcard);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }




        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd").create();


        List<Zyfj> fjxx = gson.fromJson(zyfj, new TypeToken<List<Zyfj>>(){}.getType());//房间信息

        List<String> goodsList = gson.fromJson(goods, new TypeToken<List<String>>(){}.getType());//商品信息



        ArrayList<OtherGoods> otherGoods = new ArrayList<>();
        ArrayList xfx = gson.fromJson(goods, otherGoods.getClass());//消费项
        System.out.println("xfx:"+xfx);

        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
        CheckInDao daoProxy =(CheckInDao) ProxyUtil.getDaoProxy(checkInDao);
        String ddbh="";
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        PrintWriter writer =null;
        JsonUtil jsonUtil = new JsonUtil();
        Gson gson1 = new GsonBuilder().create();
        for (int i = 0; i <fjxx.size() ; i++) {
            Zyfj zyfj1 = fjxx.get(i);
            RoomList roomList = new RoomList();
            ddbh=(System.currentTimeMillis()+"").substring(5);
            roomList.setLid(ddbh);//订单编号
            roomList.setRid(zyfj1.getRoomvalue());//房间编号
            //roomList.setSid("3");//暂时默认为现金支付
            roomList.setEid("1");//暂时默认操作员
            roomList.setLtype(ltype);
            if (timetype.equals("1")) {
                roomList.setIntime(zyfj1.getDatevalue()[0]);//入住时间
                roomList.setOuttime(zyfj1.getDatevalue()[1]);//离开时间
            }else{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                try {
                    roomList.setIntime(simpleDateFormat.parse(zyfj1.getDatevalue1()));//入住时间
                    roomList.setOuttime(simpleDateFormat.parse(zyfj1.getDatevalue1()));//离开时间
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            roomList.setLdate(new Date());//开单时间
            roomList.setLsum(Double.parseDouble(sunmoney));//总价
            roomList.setLcash(Double.parseDouble(yajin));//押金
            roomList.setLotherprice(Double.parseDouble(othermoney));//其他消费金额
            if (payitem!=null&&payitem!=""){
                roomList.setSid(payitem);//收款方式
            }

            roomList.setLillegal(0.0);//违约金
            if (rstatus.equals("预定")){
                roomList.setLinStatus("已预定");//入住状态
                roomList.setOutStatus("欠款");
            }else {
                roomList.setLinStatus("已入住");//入住状态
                roomList.setOutStatus("已收齐");
            }

            roomList.setLroomprice(Double.parseDouble(zyfj1.getMoney()));//房价

            roomList.setDeposit(1);



            try {
                writer = resp.getWriter();
                daoProxy.Rent(zyfj1.getRoomvalue(),rstatus,username,roomList,goodsList,client1,flag,timetype);//租房
                jsonUtil.setCode(0);
                jsonUtil.setMsg("成功");

            } catch (SQLException throwables) {
                throwables.printStackTrace();
                jsonUtil.setCode(1);
                jsonUtil.setMsg("系统错误");
            } catch (IOException e) {
                e.printStackTrace();
                jsonUtil.setCode(1);
                jsonUtil.setMsg("系统错误");
            }
        }
        if (writer!=null) {
            writer.print(gson1.toJson(jsonUtil));
        }



    }
//    public void ren1(String zyfj,String rstatus,String name,String phone,String idcard,String username,String goods,String sunmoney,String out_status,String othermoney,String yajin,String ltype,String payitem){
//
//        Boolean flag=true;//真:存在客户
//        CheckInDaoImpl checkInDao1 = new CheckInDaoImpl();
//        Client client1 =null;
//        try {
//            client1 = checkInDao1.SelectIdCard(idcard);
//            if (client1==null){
//                flag=false;
//                //不存在则新创建顾客
//                client1 = new Client();
//                client1.setPno((System.currentTimeMillis()+"").substring(5));
//                client1.setCliPid("1");
//                client1.setPname(name);
//                client1.setPphone(phone);
//                client1.setPid(idcard);
//                client1.setPtotal(Double.parseDouble(sunmoney));
//                client1.setPcount(1);
//                client1.setLasttime(new Date());
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                String format = simpleDateFormat.format(new Date());
//                client1.setPcretime(format);
//
//            }else {
//                client1.setPtotal(client1.getPtotal()+Double.parseDouble(sunmoney));
//                client1.setLasttime(new Date());
//                client1.setPcount(client1.getPcount()+1);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//
//
//
//
//        GsonBuilder gsonBuilder = new GsonBuilder();
//        Gson gson = gsonBuilder.setDateFormat("yyyy-MM-dd").create();
//
//
//        List<Zyfj> fjxx = gson.fromJson(zyfj, new TypeToken<List<Zyfj>>(){}.getType());//房间信息
//
//        List<String> goodsList = gson.fromJson(goods, new TypeToken<List<String>>(){}.getType());//商品信息
//
//
//
//        ArrayList<OtherGoods> otherGoods = new ArrayList<>();
//        ArrayList xfx = gson.fromJson(goods, otherGoods.getClass());//消费项
//        System.out.println("xfx:"+xfx);
//
//        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
//        CheckInDao daoProxy =(CheckInDao) ProxyUtil.getDaoProxy(checkInDao);
//        String ddbh="";
//        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        for (int i = 0; i <fjxx.size() ; i++) {
//            Zyfj zyfj1 = fjxx.get(i);
//            RoomList roomList = new RoomList();
//            ddbh=(System.currentTimeMillis()+"").substring(5);
//            roomList.setLid(ddbh);//订单编号
//            roomList.setRid(zyfj1.getRoomvalue());//房间编号
//            //roomList.setSid("3");//暂时默认为现金支付
//            roomList.setEid("1");//暂时默认操作员
//            roomList.setLtype(ltype);
//            roomList.setIntime(zyfj1.getDatevalue()[0]);//入住时间
//            roomList.setOuttime(zyfj1.getDatevalue()[1]);//离开时间
//            roomList.setLdate(new Date());//开单时间
//            roomList.setLsum(Double.parseDouble(sunmoney));//总价
//            roomList.setLcash(Double.parseDouble(yajin));//押金
//            roomList.setLotherprice(Double.parseDouble(othermoney));//其他消费金额
//            if (payitem!=null&&payitem!=""){
//                roomList.setSid(payitem);//收款方式
//            }
//
//            roomList.setLillegal(0.0);//违约金
//            if (rstatus.equals("预定")){
//                roomList.setLinStatus("已预定");//入住状态
//                roomList.setOutStatus("欠款");
//            }else {
//                roomList.setLinStatus("已入住");//入住状态
//                roomList.setOutStatus("已收齐");
//            }
//
//            roomList.setLroomprice(Double.parseDouble(zyfj1.getMoney()));//房价
//
//            roomList.setDeposit(1);
//
//
//            try {
//                daoProxy.Rent(zyfj1.getRoomvalue(),rstatus,username,roomList,goodsList,client1,flag);//租房
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//
//
//        }
//
//
//    }

    //根据roomid查询该房订单总价格
    public void SelectTotalPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        String rid = req.getParameter("rid");
        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        PrintWriter writer =null;
        try {
            writer = resp.getWriter();
            String lid = checkInDao.SelectLidWererid(rid);//得到订单编号
            RoomList roomList = checkInDao.SelectLidWererTotalPrice(lid);
            HashMap<String, Double> Map = new HashMap<>();
            Map.put("lroomprice",roomList.getLroomprice());
            Map.put("lotherprice",roomList.getLotherprice());
            Map.put("lsum",roomList.getLsum());
            Map.put("lcash",roomList.getLcash());
            jsonUtil.setCode(0);
            jsonUtil.setMsg("ok");
            jsonUtil.setData(Map);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");

        }finally {
            writer.print(gson.toJson(jsonUtil));
        }


    }

    //确定入住(在状态已经是预定的情况下，确定缴费入住)
    public void qdrz(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        String paytype = req.getParameter("paytype");

        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
        CheckInDao daoProxy =(CheckInDao) ProxyUtil.getDaoProxy(checkInDao);
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        PrintWriter writer =null;
        try {
            writer=resp.getWriter();
            String lid = checkInDao.SelectLidWererid(rid);//得到订单编号
            daoProxy.updatedd(lid,rid,paytype);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("ok");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }finally {
            writer.print(gson.toJson(jsonUtil));
        }


    }

    //退房
    public void checkout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");

        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
        CheckInDao daoProxy =(CheckInDao) ProxyUtil.getDaoProxy(checkInDao);
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        PrintWriter writer =null;
        try {
            writer=resp.getWriter();
            String lid = checkInDao.SelectLidWererid(rid);//得到订单编号
            daoProxy.updateddtozan(lid,rid);//更新订单and房间状态
            Double selectlcash = checkInDao.Selectlcash(lid);//返回应退押金

            jsonUtil.setCode(0);
            jsonUtil.setMsg("ok");
            HashMap<String, Double> map = new HashMap<>();
            map.put("selectlcash",selectlcash);
            jsonUtil.setData(map);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }finally {
            writer.print(gson.toJson(jsonUtil));
        }

    }
    //打扫房间(更改房间状态)
    public void sweep(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");

        CheckInDaoImpl checkInDao = new CheckInDaoImpl();
        CheckInDao daoProxy =(CheckInDao) ProxyUtil.getDaoProxy(checkInDao);
        Gson gson = new GsonBuilder().create();
        JsonUtil jsonUtil = new JsonUtil();
        PrintWriter writer =null;
        try {
            writer=resp.getWriter();

            daoProxy.updateddtokon(rid);//更新房间状态

            jsonUtil.setCode(0);
            jsonUtil.setMsg("ok");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }finally {
            writer.print(gson.toJson(jsonUtil));
        }
    }
}
